package cn.lingyangwl.agile.misc.order.service;

import cn.lingyangwl.agile.misc.order.assembly.*;
import cn.lingyangwl.agile.misc.order.mapper.*;
import cn.lingyangwl.agile.misc.order.model.*;
import cn.lingyangwl.agile.misc.order.model.entity.*;
import cn.lingyangwl.framework.mybatis.plus.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 订单详情
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
    implements IService<OrderDetail>, OrderDetailService {

    @Resource
    private OrderDetailAssembly orderDetailAssembly;
    
    @Override
    public IPage<OrderDetailResp> pageOrderDetail(OrderDetailQueryReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<OrderDetail> lqw = OrderDetail.lqw()
            .ge(Objects.nonNull(beginTime), OrderDetail::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), OrderDetail::getCreatedAt, endTime)
            .orderByDesc(OrderDetail::getCreatedAt);
        IPage<OrderDetail> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, orderDetailAssembly::toOrderDetailResp);
    }
    
    @Override
    public List<OrderDetailResp> listOrderDetail() {
    	return this.list().stream()
    	        .map(orderDetailAssembly::toOrderDetailResp).collect(Collectors.toList());
    }

    @Override
    public OrderDetailResp getOrderDetail(Long id) {
        OrderDetail entity = this.getById(id);
    	return orderDetailAssembly.toOrderDetailResp(entity);
    }

    @Override
    public boolean saveOrderDetail(OrderDetailSaveReq req) {
        OrderDetail entity = orderDetailAssembly.toOrderDetail(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateOrderDetail(OrderDetailSaveReq req) {
        OrderDetail entity = orderDetailAssembly.toOrderDetail(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteOrderDetail(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteOrderDetail(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
